<template>
    <div class="layout-container">
        <div class="layout-container-left">
            <UseTable :tableData="tableData" unique @change="change" />
        </div>

        <div class="layout-container-right">
            <div class="form-box">
                <el-form label-width="auto">
                    <el-form-item label="分组数量">
                        <el-input v-model="paramsData.groupCount" type="number" placeholder="请输入组数" />
                    </el-form-item>

                    <el-form-item>
                        <div class="data-select">
                            <DmData @dataChange="dataChange" />
                            <Upload @uploadSuccess="uploadSuccess" />
                        </div>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="handleCalculate">计算</el-button>
                    </el-form-item>
                </el-form>
            </div>

            <el-divider />

            <div class="info-box">
                <h5>算法说明</h5>
                <p>通过最大化组间差异、最小化组内差异确定分组边界（Jenks 优化法），使每组数据更具同质性。</p>

                <h5>使用范围</h5>
                <ul>
                    <li>数据存在自然聚类的场景（如城市人口分布的 “密集区 - 稀疏区”）；</li>
                    <li>避免人为划分的主观性（如按自然断点划分地震烈度等级）；</li>
                    <li>适合呈现数据的内在分布特征。</li>
                </ul>
            </div>
        </div>

        <Result v-model="resultShow" :originalData="paramsData" :result="resultData" />
    </div>
</template>

<script setup lang="ts">
import {onActivated, onMounted, ref} from "vue"
import {ElMessage}                   from "element-plus"
import {naturalBreaks}               from "@/api/grouping"
import Result                        from "./components/Result.vue"
import {getExcel}                    from "@/api/data"
import Upload                        from "@/components/Upload.vue"
import UseTable                      from "@/components/UseTable.vue"
import DmData                        from "@/components/DmData.vue"

function dataChange(data) {
    tableData.value = data
}

function uploadSuccess(res) {
    tableData.value = res.data
}

function change(list) {
    paramsData.value.data = list?.[0]?.value || []
}

let paramsData = ref({
    data      : [],
    groupCount: 2
})

let resultShow = ref(false)
let resultData = ref({})

function handleCalculate() {
    if (!paramsData.value.data || paramsData.value.data.length === 0) {
        ElMessage.warning("请输入参数")
        return
    }

    if (!paramsData.value.groupCount) {
        ElMessage.warning("请输入参数")
        return
    }

    naturalBreaks(paramsData.value).then((res: any) => {
        resultShow.value = true
        resultData.value = res?.data
    })
}

let loading   = ref(true)
let tableData = ref([])

let getTableData = () => {
    loading.value = true

    getExcel("natural-breaks").then(res => res.data).then(data => tableData.value = data)

    loading.value = false
}

/**
 * 初始化参数
 */
function initParam() {

}

// -----------------------------主程序start-----------------------------------------
onMounted(() => {
    initParam()
    getTableData()
})

onActivated(() => {
    initParam()
    getTableData()
})
// -----------------------------主程序end-----------------------------------------

</script>

<style scoped>

</style>

